package com.cicoding.controller;


import com.cicoding.service.KufangService;
import com.cicoding.service.Uf3sReaderService;
import com.cicoding.uhfreader.CabinetModeReadData;
import com.cicoding.uhfreader.ReaderAsyncRead;
import com.cicoding.uhfreader.ReaderAsyncReadData;
import com.cicoding.uhfreader.RscjaAsyncRead;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/invent")
public class InventoryController {

    @Autowired
    private ReaderAsyncRead readerAsyncRead;

    @Autowired
    private ReaderAsyncReadData readerAsyncReadData;

    @Autowired
    private RscjaAsyncRead rscjaAsyncRead;

    @Autowired
    private Uf3sReaderService uf3sReaderService;


    @Autowired
    private KufangService kufangService;

    private final ExecutorService executorService = Executors.newSingleThreadExecutor();


    @GetMapping(value = "/getLatest")
    public void getLatest(){
        readerAsyncRead.openReader();

    /*    executorService.submit(() -> {
            readerAsyncRead.openReader();
            readerAsyncRead.checkGpi();
            // 如果需要，你可以在这里执行其他的异步操作
        });*/
        readerAsyncRead.startReadTags();
    }

    @Component
    public static class MyCommandLineRunner implements CommandLineRunner {
     //   private final ReaderAsyncRead readerAsyncRead;

        private final ReaderAsyncReadData readerAsyncReadData;

      //  private final CabinetModeReadData cabinetModeReadData;

      /*  @Autowired
        public MyCommandLineRunner(ReaderAsyncRead readerAsyncRead) {
            this.readerAsyncRead = readerAsyncRead;
        }*/

        @Autowired
        public MyCommandLineRunner(ReaderAsyncReadData readerAsyncReadData) {
            this.readerAsyncReadData = readerAsyncReadData;
        }

       /* @Autowired
        public MyCommandLineRunner(CabinetModeReadData cabinetModeReadData) {
            this.cabinetModeReadData = cabinetModeReadData;
        }*/

        @Override
        public void run(String... args) throws Exception {
            readerAsyncReadData.openReader();
            readerAsyncReadData.scheduleTaskWithDelay(20);
            readerAsyncReadData.query();
            // 在这里编写启动完成后需要执行的逻辑
           /* while (true){
                readerAsyncReadData.checkGpis1();
                Thread.sleep(100); // 每秒钟检查一次
            }*/
        }

       /* @Override
        public void run(String... args) throws Exception {
            readerAsyncRead.openReader();
            readerAsyncRead.scheduleTaskWithDelay(20);
            readerAsyncRead.query();
            // 在这里编写启动完成后需要执行的逻辑
            while (true){
                readerAsyncRead.checkGpis1();
                Thread.sleep(100); // 每秒钟检查一次
            }
        }*/
    }

//    @Component
//    public static class MyCommandLineRunner implements CommandLineRunner {
//        private final RscjaAsyncRead readerAsyncReadData;
//
//        @Autowired
//        public MyCommandLineRunner(RscjaAsyncRead readerAsyncReadData) {
//            this.readerAsyncReadData = readerAsyncReadData;
//        }
//        @Override
//        public void run(String... args) throws Exception {
//            readerAsyncReadData.initReds();
//            readerAsyncReadData.scheduleTaskWithDelay(20);
//            readerAsyncReadData.query();
//            // 在这里编写启动完成后需要执行的逻辑
//        }
//    }

   /* @Component
    public static class MyCommandLineRunner implements CommandLineRunner {
        private final Uf3sReaderService uf3sReaderService;

        @Autowired
        public MyCommandLineRunner(Uf3sReaderService uf3sReaderService) {
            this.uf3sReaderService = uf3sReaderService;
        }
        @Override
        public void run(String... args) throws Exception {
           // uf3sReaderService.();
            uf3sReaderService.startClient();
            uf3sReaderService.scheduleTaskWithDelay(20);
            uf3sReaderService.query();
            // 在这里编写启动完成后需要执行的逻辑
        }
    }*/

    @GetMapping(value = "/getAsyncReadData")
    public void getAsyncReadData()
    {

        readerAsyncReadData.scheduleTaskWithDelayAndClose(8);
    }

    @GetMapping(value = "/getLatest1")
    public void getLatest1()
    {
        rscjaAsyncRead.scheduleTaskWithDelayAndClose(10);
    }


    @GetMapping(value = "/uf3sReaderService")
    public void uf3sReaderService()
    {
        uf3sReaderService.scheduleTaskWithDelayAndClose(10);
    }
}
